package local.pt.iscte.dcti.pa.model;

import java.util.List;
import java.util.Observer;

/**
 * 	Defines the model
 *  @author Simão Fernandes
 *  @version %I%, %G%
 *  @since 1.0
 */
public class WorksheetModel {

	//ATTRIBUTES
	
	/**
	 * Defines a worksheet
	 */
	private Cell[][] sheet;
	/**
	 * Names of the functions provided by the plugins
	 */
	
	//CONSTRUCTORS
	
	/**
	 *	Class constructor specifying number of sheet positions 
	 * 	@param _numRows		Number of sheet rows
	 * 	@param _numColumns	Number of sheet columns
	 */
	public WorksheetModel(int _numRows, int _numColumns) {
		
		
		local.pt.iscte.dcti.pa.utils.ExtensionManager myExtentionManager =
				new local.pt.iscte.dcti.pa.utils.ExtensionManager();
		
		sheet = new Cell[_numRows][_numColumns];
		
		for (int i = 0; i < _numRows; i++) {
			for (int j = 0; j < _numColumns; j++) {
				sheet[i][j] = new Cell(i, j, this, myExtentionManager);
			}
		}
	}
	
	//PUBLIC METHODS
	
	/**
	 *	Add one observer for each sheet position
	 * 	@param observadores	Set of observers who will what the corresponding sheet position
	 */
	public void addObserver(Observer[][] observadores) {
		for (int i = 0; i < observadores.length; i++) {
			for (int j = 0; j < observadores[i].length; j++) {
				this.sheet[i][j].addObserver(observadores[i][j]);
			}
		}
	}
	
	/**
	 * Gets the value of a cell
	 * @param abscissa		Cell abscissa
	 * @param ordinate		Cell ordinate
	 * @return				Cell value
	 * @throws Exception	Thrown exception
	 */
	public String getCellValue(int abscissa, int ordinate) throws Exception {
		return getCell(abscissa, ordinate).getValue();
	}
	
	/**
	 * Gets the reference for a cell
	 * @param abscissa	Cell abscissa
	 * @param ordinate	Cell ordinate
	 * @return			Cell reference
	 */
	public Cell getCell(int abscissa, int ordinate) {
		return sheet[abscissa][ordinate];
	}
	
	/**
	 * Calculates the value of a cell
	 * @param xx						Cell abscissa
	 * @param yy						Cell ordinate
	 * @param value						Cell value
	 * @return							Result
	 * @throws NumberFormatException	thrown exception
	 * @throws Exception				thrown exception
	 */
	public String work (int xx, int yy, String value) throws NumberFormatException, Exception {
		
		Cell c = sheet[xx][yy];
		//Actualizar o modelo
		c.setValue(value);
		return c.solveIt();
		
	}	
}
